package com.qingzhuge.file.service.impl;

import com.qingzhuge.file.dao.SysFileDao;
import com.qingzhuge.file.entity.SysFile;
import com.qingzhuge.file.service.ISysFileService;
import com.qingzhuge.service.BasePkLongServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.File;


/**
 * @author : zero.xiao
 * @description :
 * @date :2018/9/20 0020 上午 9:35
 * @since : 1.0.0
 * @modified :
 */
@Service
public class SysFileServiceImpl extends BasePkLongServiceImpl<SysFileDao, SysFile> implements ISysFileService {


    @Override
    public SysFile findByUrl(String url) {
        return baseDao.findByUrl(url);
    }

    @Override
    public void deleteById(Long id) {
        SysFile sysFile = super.findById(id);
        if (sysFile != null) {
            boolean deleteFile = true;
            if (StringUtils.isNotBlank(sysFile.getUrl())) {
                File file = new File(sysFile.getUrl());
                if (file.getParentFile().exists() && file.exists() && file.isFile()) {
                    deleteFile = file.delete();
                }
            }
            if(deleteFile) {
                super.deleteById(id);
            }
        }
    }

    @Override
    public SysFile findByIdAndAuthType(Long id,Integer type){
        return baseDao.findByIdAndAuthType(id,type);
    }
}
